// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#pragma once

#include <memory>
#include <string>
#include <vector>

namespace kumo::pollux::core {
    class IExpr;
    class SortOrder;
} // namespace kumo::pollux::core

namespace kumo::pollux::duckdb {
    /// Hold parsing options.
    struct ParseOptions {
        // Retain legacy behavior by default.
        bool parseDecimalAsDouble = true;
        bool parseIntegerAsBigint = true;

        /// SQL functions could be registered with different prefixes by the user.
        /// This parameter is the registered prefix of presto or spark functions,
        /// which helps generate the correct Pollux expression.
        std::string functionPrefix;
    };

    // Parses an input expression using DuckDB's internal postgresql-based parser,
    // converting it to an IExpr tree. Takes a single expression as input.
    //
    // One caveat to keep in mind when using DuckDB's parser is that all identifiers
    // are lower-cased, what prevents you to use functions and column names
    // containing upper case letters (e.g: "concatRow" will be parsed as
    // "concatrow").
    std::shared_ptr<const core::IExpr> parseExpr(
        const std::string &exprString,
        const ParseOptions &options);

    std::vector<std::shared_ptr<const core::IExpr> > parseMultipleExpressions(
        const std::string &exprString,
        const ParseOptions &options);

    struct AggregateExpr {
        std::shared_ptr<const core::IExpr> expr;
        std::vector<std::pair<std::shared_ptr<const core::IExpr>, core::SortOrder> >
        orderBy;
        bool distinct{false};
        std::shared_ptr<const core::IExpr> maskExpr{nullptr};
    };

    /// Parses aggregate function call expression with optional ORDER by clause.
    /// Examples:
    ///     sum(a)
    ///     sum(a) as s
    ///     array_agg(x ORDER BY y DESC)
    AggregateExpr parseAggregateExpr(
        const std::string &exprString,
        const ParseOptions &options);

    // Parses an ORDER BY clause using DuckDB's internal postgresql-based parser,
    // converting it to a pair of an IExpr tree and a core::SortOrder. Uses ASC
    // NULLS LAST as the default sort order.
    std::pair<std::shared_ptr<const core::IExpr>, core::SortOrder> parseOrderByExpr(
        const std::string &exprString);

    // Parses a WINDOW function SQL string using DuckDB's internal postgresql-based
    // parser. Window Functions are executed by Pollux Window PlanNodes and not the
    // expression evaluation. So we cannot use an IExpr based API. The structures
    // below capture all the metadata needed from the window function SQL string
    // for usage in the WindowNode plan node.
    enum class WindowType { kRows, kRange };

    enum class BoundType {
        kCurrentRow,
        kUnboundedPreceding,
        kUnboundedFollowing,
        kPreceding,
        kFollowing
    };

    struct IExprWindowFrame {
        WindowType type;
        BoundType startType;
        std::shared_ptr<const core::IExpr> startValue;
        BoundType endType;
        std::shared_ptr<const core::IExpr> endValue;
    };

    struct IExprWindowFunction {
        std::shared_ptr<const core::IExpr> functionCall;
        IExprWindowFrame frame;
        bool ignoreNulls;

        std::vector<std::shared_ptr<const core::IExpr> > partitionBy;
        std::vector<std::pair<std::shared_ptr<const core::IExpr>, core::SortOrder> >
        orderBy;
    };

    IExprWindowFunction parseWindowExpr(
        const std::string &windowString,
        const ParseOptions &options);
} // namespace kumo::pollux::duckdb
